package com.mapre.instance.multi;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;

/**
 * @author shengtao
 * @Description:
 * @date 2019/01/14 15:14
 */
public class MultiOutput extends Configured implements Tool {

    public static void main(String[] args) throws Exception {
        ToolRunner.run(new MultiOutput(),args);

        System.out.println("processed result is not success！");
    }

    @Override
    public int run(String[] args) throws Exception {
        String nameNodeUrl = "hdfs://192.168.220.135:9000";
        String nameNodeName = "fs.defaultFS";
        Configuration configuration = new Configuration();
        configuration.set(nameNodeName, nameNodeUrl);
        Job job = Job.getInstance(configuration);
        job.setJarByClass(MultiOutput.class);

        job.setMapperClass(MultiMapper.class);
        job.setReducerClass(MultiResucer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(Text.class);

        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        boolean result = job.waitForCompletion(true);
        if (!result){
            return 1;
        }
        return 0;
    }

    static class MultiMapper extends Mapper<LongWritable,Text,Text,Text>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String s[] = line.split("\t");
            context.write(new Text(s[0]),value);
        }
    }

    static class MultiResucer extends Reducer<Text,Text,NullWritable,Text>{

        private MultipleOutputs<NullWritable,Text> multipleOutputs;

        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            multipleOutputs = new MultipleOutputs<>(context);
        }

        @Override
        protected void cleanup(Context context) throws IOException, InterruptedException {
            multipleOutputs.close();
        }

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            for (Text value:values){
                multipleOutputs.write(NullWritable.get(),value,key.toString());
            }
        }
    }



}
